﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lottery.Entity;
using Lottery.DataAccess;
using System.Text.RegularExpressions;
using System.Text;

public partial class Football_BJSingle_Shrink : System.Web.UI.Page
{

    Dictionary<string, int> dicResult = new Dictionary<string, int>();

    int countTotal = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void test()
    {

        string result = ResultCollection.GetResultString();

        GetResultDictionary(result, 5);


        List<ModIssueResultString> issueResultSet =
            ResultCollection.GetIssueResultString().Where(r => r.IssueNumber > 20110300).ToList();

        Dictionary<int, int> list = new Dictionary<int, int>();

        foreach (ModIssueResultString mod in issueResultSet)
        {
            countTotal = 0;
            int count = Calculation(mod.Result, 0);
            int i = count / (mod.Result.Length - 5);
            list.Add(mod.IssueNumber, i);
        }

        StringBuilder output = new StringBuilder();
        foreach (KeyValuePair<int, int> k in list)
        {
            output.AppendFormat("{0} -- {1}<br />", k.Key.ToString(), k.Value);
        }

        Response.Write(output.ToString());
    }

    private int Calculation(string result, int count)
    {
        result = result.Replace("4", string.Empty);
        if (result.Length > 5)
        {
            string str = result.Substring(0, 5);
            if (dicResult.ContainsKey(str))
            {
                count += Calculation(result.Remove(0, 1), dicResult[str]);
            }
        }
        return count;
    }

    private void GetResultDictionary(string result, int length)
    {
        string tempResult = result;

        while (tempResult.Length > length)
        {
            string str = tempResult.Substring(0, length);
            if (str.IndexOf("4") < 0)
            {
                int appearTime = Regex.Matches(result, str).Count;

                if (!dicResult.ContainsKey(str))
                {
                    dicResult.Add(str, appearTime);
                }
            }
            tempResult = tempResult.Remove(0, 1);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        test();
    }
}
